package projects;

import getData.GetDataFromProjectXML;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class DoOperationsWithProjects extends WriteGroupsToXMLwithProjects {

	protected static String nameOfNewProject = "NewProject";
	protected static int indexOfProjectToDelete = 0;

	//public static void renamePoject(int indexOfProjectToDelete, String nameOfNewProjectForRename)
		//	throws IOException, TransformerException {
				//checkForDuplicateProject(nameOfNewProjectForRename);
				//deleteProject (indexOfProjectToDelete);
				//addProject(nameOfNewProjectForRename,listOfProjectsFileName);
			//}

	public static void deleteProject(int indexOfProjectToDelete, String listOfProjectsFileName) {
		//System.out.println(listOfProjects);
		//System.out.println(indexOfProjectToDelete);
		listOfProjects.remove(indexOfProjectToDelete);
		quantityOfProjectsinList--;
		Collections.sort(listOfProjects);
		//System.out.println(listOfProjects);
		updateProjectListXML(listOfProjects,listOfProjectsFileName);
	}

	public static boolean addProject(String nameOfNewProject, String listOfProjectsFileName){
		boolean endButtonProcedure = false;
		endButtonProcedure = checkForDuplicateProject(nameOfNewProject, endButtonProcedure);
		//System.out.println(endButtonProcedure);
		
		if (endButtonProcedure == false){
		//System.out.println(listOfProjects);
		
		listOfProjects.add(nameOfNewProject);
		quantityOfProjectsinList++;
		
		Collections.sort(listOfProjects);
		//System.out.println(quantityOfProjectsinList);
		//System.out.println(listOfProjects);
		updateProjectListXML(listOfProjects,listOfProjectsFileName);
		Collections.sort(listOfProjects);
		//System.out.println(quantityOfProjectsinList);
	}
		return endButtonProcedure;
	}
	
	public static boolean checkForDuplicateProject(String nameOfNewProject, boolean endButtonProcedure) {
		
		for (String item:listOfProjects) {
			if (item.equals(nameOfNewProject)){
				//System.out.println();
				   JOptionPane.showMessageDialog(new JFrame(), "Project with name '" + 
							nameOfNewProject + "'is already exists", "Error",
					        JOptionPane.ERROR_MESSAGE);
				 endButtonProcedure = true;	   
			}
		}
		//System.out.println(endButtonProcedure);
		return endButtonProcedure;
	}

	public static boolean checkForDuplicateFrame(String nameOfNewFrame,
			boolean endButtonProcedure) {
		for (String item:listOfFrames) {
			if (item.equals(nameOfNewFrame)){
				//System.out.println();
				   JOptionPane.showMessageDialog(new JFrame(), "Frame with name '" + 
							nameOfNewProject + "'is already exists in this project", "Error",
					        JOptionPane.ERROR_MESSAGE);
				 endButtonProcedure = true;	   
			}
		}
		//System.out.println(endButtonProcedure);
		return endButtonProcedure;
	}
	
	public DoOperationsWithProjects() {
		super();
	}

	@SuppressWarnings("static-access")
	public static String[] getListOfFramesForChoosedProject
	(List<String> listOfProjects, String listOfProjectsFileName, 
	String nameOfProjectToDelete, 
	List<String> listOfFrames, 
	String[] listOfFramesForEditedProject){
		GetDataFromProjectXML getGroupsFromXML = new GetDataFromProjectXML();
		for (String item:listOfProjects) {
					if (item == nameOfProjectToDelete)
					listOfFrames =  new ArrayList<String>();	
					//System.out.println("listOfProjectsFileName 2 " + listOfProjectsFileName);
					listOfFrames = getGroupsFromXML.getOldListOfFramesInCurrentProject(item, listOfProjectsFileName);
					listOfFramesForEditedProject = listOfFrames.toArray(new String[0]);
				}
		return listOfFramesForEditedProject;
	}
}